<?php
//
//  VictoryCMS - Content managment system and framework.
//  Copyright (C) 2009  Lewis Gunsch
//
//  This file is part of VictoryCMS.
//  
//  VictoryCMS is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  VictoryCMS is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License
//  along with VictoryCMS.  If not, see <http://www.gnu.org/licenses/>.

/**
 * VictoryCMS - Form
 * 
 * @license http://www.gnu.org/licenses/gpl.html
 * @author Lewis Gunsch
 * @filesource
 * @package HtmlBuilders
 */

/**
 * This HtmlBuilder extension renders html forms. 
 * The html forms are rendered into a HtmlList.
 * 
 * @package HtmlBuilders
 * 
 * @todo Needs documentation finished
 * @todo Finish implementation of form XHTML tags.
 */
class Form extends HtmlBuilder {
	
	public function __construct(HtmlList &$list) {
		parent::__construct($list);
	}
	
	public function __destruct() {
		parent::__destruct();
	}
	
	/**
	 * @todo Make list, and make application/x-www-form-urlencoded the default
	 * 
	 * @param array $action
	 * @param $attributes
	 * @return void
	 */
	public function beginForm($action, Array $attributes) {	
		//TODO: Make list, and make application/x-www-form-urlencoded the default
		
		$html = '<form action="'.$action.'" ';
		if (!empty($attributes['method'])) {
			$html .= ' method="'.$attributes['method'].'"';
		}
		if (!empty($attributes['id'])) {
			$html .= ' id="'.$attributes['id'].'"';
		}
		if (!empty($attributes['enctype'])) {
			$html .= ' enctype="'.$attributes['enctype'].'"';
		}
		if (!empty($attributes['title'])) {
			$html .= ' title="'.$attributes['title'].'"';
		}
		if (!empty($attributes['class'])) {
			$html .= ' class="'.$attributes['class'].'"';
		}
		if (!empty($attributes['style'])) {
			$html .= ' style="'.$attributes['style'].'"';
		}
		$html .= '>';
		$this->list->add($html);
	}
	
	/**
	 * This pushes the ending tag of the form onto the current
	 * Html list.
	 * 
	 * @return void
	 */
	public function endForm() {
		$html = '</form>';
		$this->list->add($html);
	}
	
	/**
	 * Adds a label into the HtmlList.
	 * 
	 * @param String Id for the label
	 * @param String For value for the label 
	 * @param String Value (text) of the label.
	 * @param String CSS class string to use for this label
	 * @param String CSS style string to use for this label
	 */
	public function pushLabel($value, Array $attributes) {
		$html = '<label';
		if (!empty($attributes['for'])) {
			$html .= ' for="'.$attributes['for'].'"';
		}
		if (!empty($attributes['id'])) {
			$html .= ' id="'.$attributes['id'].'"';
		}
		if (!empty($attributes['title'])) {
			$html .= ' title="'.$attributes['title'].'"';
		}
		if (!empty($attributes['class'])) {
			$html .= ' class="'.$attributes['class'].'"';
		}
		if (!empty($attributes['style'])) {
			$html .= ' style="'.$attributes['style'].'"';
		}
		$html .= '>'.$value.'</label>';
		$this->list->add($html);
	}
	
	/**
	 * Pushes a hidden field to the form.
	 * 
	 * @param String Id for the hidden field.
	 * @param String Name value for the hidden field.
	 * @param String Value (text) of the hidden field.
	 */
	public function pushHidden(Array $attributes) {	
		$html = '<input ';
		if (!empty($attributes['type'])) {
			$html .= ' type="'.$attributes['type'].'"';
		}
		if (!empty($attributes['id'])) {
			$html .= ' id="'.$attributes['id'].'"';
		}
		if (!empty($attributes['name'])) {
			$html .= ' name="'.$attributes['name'].'"';
		}
		if (!empty($attributes['value'])) {
			$html .= ' value="'.$attributes['value'].'"';
		}
		$html .= ' />';
		$this->list->add($html);
	}
	
	/**
	 * Pushes a text field into the form array.
	 * 
	 * @param String Id for the text field.
	 * @param String Name value for the text field.
	 * @param String Value (text) of the text field.
	 * @param String CSS class string to use for this text tield.
	 * @param String CSS style string to use for this text field.
	 * @param Boolean True if text should be read only, false if not; Default is false.
	 * @param Integer Maximum number of characters; -1 to disable.
	 * @param Integer Size of field in characters; -1 to disable.
	 */
	public function pushTextInput(Array $attributes) {	
		$html = '<input ';
		if (!empty($attributes['type'])) {
			$html .= ' type="'.$attributes['type'].'"';
		}
		if (!empty($attributes['id'])) {
			$html .= ' id="'.$attributes['id'].'"';
		}
		if (!empty($attributes['name'])) {
			$html .= ' name="'.$attributes['name'].'"';
		}
		if (!empty($attributes['value'])) {
			$html .= ' value="'.$attributes['value'].'"';
		}
		if (!empty($attributes['readOnly'])) {
			$html .= ' readonly="'.$attributes['readonly'].'"';
		}
		if (!empty($attributes['maxlength'])) {
			if ($attributes['maxlength'] >= 0) {
				$html .= ' maxlength="'.$attributes['maxlength'].'"';
			}
		}
		if (!empty($attributes['size'])) {
			if ($attributes['size'] >= 0) {
				$html .= ' size="'.$attributes['size'].'"';
			}
		}
		if (!empty($attributes['title'])) {
			$html .= ' title="'.$attributes['title'].'"';
		}
		if (!empty($attributes['class'])) {
			$html .= ' class="'.$attributes['class'].'"';
		}
		if (!empty($attributes['style'])) {
			$html .= ' style="'.$attributes['style'].'"';
		}
		$html .= ' />';
		$this->list->add($html);
	}	
	
	/**
	 * Pushes a file field into the form.
	 * 
	 * @param String Id for the text field.
	 * @param String Name value for the text field.
	 * @param String CSS class string to use for this text tield.
	 * @param String CSS style string to use for this text field.
	 * @param Integer Size of field in characters; -1 to disable.
	 * @param String Accept list content types; leave empty to disable. 
	 */
	public function pushFileInput(Array $attributes) {
		$html = '<input ';
		if (!empty($attributes['type'])) {
			$html .= ' type="'.$attributes['type'].'"';
		}
		if (!empty($attributes['id'])) {
			$html .= ' id="'.$attributes['id'].'"';
		}
		if (!empty($attributes['name'])) {
			$html .= ' name="'.$attributes['name'].'"';
		}
		if (!empty($attributes['value'])) {
			$html .= ' value="'.$attributes['value'].'"';
		}
		if (!empty($attributes['title'])) {
			$html .= ' title="'.$attributes['title'].'"';
		}
		if (!empty($attributes['class'])) {
			$html .= ' class="'.$attributes['class'].'"';
		}
		if (!empty($attributes['style'])) {
			$html .= ' style="'.$attributes['style'].'"';
		}	
		if (!empty($attributes['size'])) {
			if ($attributes['size'] >= 0) {
				$html .= ' size="'.$attributes['size'].'"';
			}
		}
		if (!empty($attributes['accept'])) {
			$html .= ' accept="'.$attributes['accept'].'"';
		}
		$html .= ' />';
		$this->list->add($html);
	}
	
	/**
	 * Pushes a submit button to the form.
	 * 
	 * @param String Id for the submit button.
	 * @param String Value for the submit button.
	 * @param String CSS class string to use for this submit button.
	 * @param String CSS style string to use for this submit button.
	 */
	public function pushSubmitInput(Array $attributes) {
		$html = '<input ';
		if (!empty($attributes['type'])) {
			$html .= ' type="'.$attributes['type'].'"';
		}
		if (!empty($attributes['id'])) {
			$html .= ' id="'.$attributes['id'].'"';
		}
		if (!empty($attributes['value'])) {
			$html .= ' value="'.$attributes['value'].'"';
		}
		if (!empty($attributes['title'])) {
			$html .= ' title="'.$attributes['title'].'"';
		}
		if (!empty($attributes['class'])) {
			$html .= ' class="'.$attributes['class'].'"';
		}
		if (!empty($attributes['style'])) {
			$html .= ' style="'.$attributes['style'].'"';
		}	
		$html .= ' />';
		$this->list->add($html);
	}
	
	/**
	 * @brief Pushes a break to the form.
	 * 
	 * @details Pushes a break (<br />) to the form.
	 * 
	 * @todo This is in the wrong class, move me
	 */
	public function pushBreak() {
		$this->list->add('<br />');
	}
	
	/**
	 * Pushes html text to the form to be rendered
	 * when the form is rendered.
	 * 
	 * @param String Html text to be pushed into the form.
	 * 
	 * @todo This is in the wrong class, move me
	 */
	public function pushAnonymousElement( $html ) {
		$this->list->add($html);
	}
	
	/**
	 * Pushes the opening tag of a select box to the 
	 * form.
	 * 
	 * @param String Id for the select box.
	 * @param String Name value for the select box.
	 * @param String CSS class string to use for this select box.
	 * @param String CSS style string to use for this select box.
	 * @param Boolean True if multiple items can be selected, default is false.
	 * @param Integer Number of visible items in the drop down select box.
	 */
	public function beginSelect(Array $attributes) {
		$html = '<select ';
		if (!empty($attributes['id'])) {
			$html .= ' id="'.$attributes['id'].'"';
		}
		if (!empty($attributes['name'])) {
			$html .= ' name="'.$attributes['name'].'"';
		}
		if (!empty($attributes['multiple'])) {
			$html .= ' multiple="'.$attributes['multiple'].'"';
		}
		if (!empty($attributes['title'])) {
			$html .= ' title="'.$attributes['title'].'"';
		}
		if (!empty($attributes['class'])) {
			$html .= ' class="'.$attributes['class'].'"';
		}
		if (!empty($attributes['style'])) {
			$html .= ' style="'.$attributes['style'].'"';
		}	
		if (!empty($attributes['size'])) {
			if ($attributes['size'] >= 0) {
				$html .= ' size="'.$attributes['size'].'"';
			}
		}
		$html .= '>';
		$this->list->add($html);
	}
	
	/**
	 * Pushes an drop down list option to a 
	 * select list.
	 * 
	 * @param $id Id for the option.
	 * @param $value Value for the option.
	 * @param $label Identifying text displayed for the option.
	 * @param $class CSS class string to use for this option.
	 * @param $style CSS style string to use for this option.
	 * @param $title Title tool text tip to use for this option.
	 * @param $disabled True if option is disabled, default is false.
	 * @param $selected True if option is selected, default is false.
	 */
	public function pushOption($text, Array $attributes) {
		$html = '<option ';
		if (!empty($attributes['id'])) {
			$html .= ' id="'.$attributes['id'].'"';
		}
		if (!empty($attributes['value'])) {
			$html .= ' value="'.$attributes['value'].'"';
		}
		if (!empty($attributes['title'])) {
			$html .= ' title="'.$attributes['title'].'"';
		}
		if (!empty($attributes['class'])) {
			$html .= ' class="'.$attributes['class'].'"';
		}
		if (!empty($attributes['style'])) {
			$html .= ' style="'.$attributes['style'].'"';
		}	
		if (!empty($attributes['disabled'])) {
			$html .= ' disabled="'.$attributes['disabled'].'"';
		}
		if (!empty($attributes['selected'])) {
			$html .= ' selected="'.$attributes['selected'].'"';
		}
		if (!empty($attributes['label'])) {
			$html .= ' label="'.$attributes['label'].'"';
		}
		$html .= '>'. $text .'</option>';
		$this->list->add($html);
	}
	
	public function endSelect() {
		$html = '</select>';
		$this->list->add($html);
	}
	
	public function beginFieldSet(Array $attributes) {
		$html = '<fieldset';
		if (!empty($attributes['id'])) {
			$html .= ' id="'.$attributes['id'].'"';
		}
		if (!empty($attributes['title'])) {
			$html .= ' title="'.$attributes['title'].'"';
		}
		if (!empty($attributes['class'])) {
			$html .= ' class="'.$attributes['class'].'"';
		}
		if (!empty($attributes['style'])) {
			$html .= ' style="'.$attributes['style'].'"';
		}	
		$html .= '>';
		$this->list->add($html);
	}
	
	public function pushLegend($value, Array $attributes) {
		$html = '<legend';
		if (!empty($attributes['id'])) {
			$html .= ' id="'.$attributes['id'].'"';
		}
		if (!empty($attributes['title'])) {
			$html .= ' title="'.$attributes['title'].'"';
		}
		if (!empty($attributes['class'])) {
			$html .= ' class="'.$attributes['class'].'"';
		}
		if (!empty($attributes['style'])) {
			$html .= ' style="'.$attributes['style'].'"';
		}	
		$html .= '>'.$value.'</legend>';
		$this->list->add($html);
	}
	
	public function endFieldSet() {
		$html = '</fieldset>';
		$this->list->add($html);
	}
	
}
?>

